package com.itheima.edu.manager.dao;

import com.itheima.edu.manager.domain.Student;

public class StudentDao {

    private static Student[] students = new Student[5];

    public boolean addStudent(Student student) {

        int index = -1;
        for (int i = 0; i < students.length; i++) {
            Student stus = students[i];
            if (stus == null) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            return false;
        } else {
            students[index] = student;
            return true;
        }


    }

    public Student[] findAllStudent() {
        return students;
    }

    public void deleteStudentById(String deleteID) {
        int index=getIndex(deleteID);
        students[index]=null;
    }

    private int getIndex(String deleteID) {

        int index=-1;
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (null!=student&&student.getId().equals(deleteID)) {
                index=i;
                break;
            }
        }
        return index;
    }

    public void updateStudent(String updateID, Student newStudent) {
        int index=getIndex(updateID);
        students[index]=newStudent;
    }
}
